﻿using System;
using System.Reflection;
using System.Text;

namespace Common.Reflection.DAL.Tools
{
    /// <summary>
    /// 把实体类反射成JS文件的帮助类
    /// </summary>
    public class EntityHelper
    {

        /// <summary>
        /// 得到验证对象的JS脚本（名叫Validate()的一个方法,返回true/false）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string GetValidateString(object obj)
        {
            Type type = obj.GetType();
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script type=\"text/javascript\">");
            sb.AppendLine("function Validate(){");
            PropertyInfo[] myPropertyInfo = type.GetProperties();
            sb.AppendLine("var f=true");
            foreach (PropertyInfo p in myPropertyInfo)
            {
                sb.AppendLine("var " + p.Name + "=$(\"#" + p.Name + "\").val();");
                AppendRequiredText(sb, p, obj);
                AppendDecimalText(sb, p, obj);
                AppendEmailText(sb, p, obj);
                AppendNumberText(sb, p, obj);
                AppendMaxValueText(sb, p, obj);
                AppendMinValueText(sb, p, obj);
                AppendMaxLengthText(sb, p, obj);
                AppendMinLengthText(sb, p, obj);
                AppendURLText(sb, p, obj);
                AppendZipCodeText(sb, p, obj);
                AppendDoubleText(sb, p, obj);
                AppendTimeText(sb, p, obj);
                AppendDateText(sb, p, obj);
                AppendMobileText(sb, p, obj);
                AppendIDCardText(sb, p, obj);
                AppendMoneyText(sb, p, obj);
            }
            sb.AppendLine("return f");
            sb.AppendLine("}");
            sb.AppendLine("</script>");
            return sb.ToString();
        }




        /// <summary>
        /// 为JS文件添加必填项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendRequiredText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Require), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("if(" + p.Name + "==\"\"){");
                sb.AppendLine("jAlert(\"" + Entity.GetRequireMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("  return false;");
                sb.AppendLine("}");
            }
            return sb;
        }




        /// <summary>
        /// 为JS文件添加最大长度项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendMaxLengthText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.MaxLength), true);
            if (objs.Length > 0)
            {
                Reflection.Model.Attribute.MaxLength v = objs[0] as Reflection.Model.Attribute.MaxLength;
                sb.AppendLine("if(" + p.Name + ".length>" + v.TheLength + "){");
                sb.AppendLine("jAlert(\"" + Entity.GetMaxLengthMessage(p, obj, false) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("  return false;");
                sb.AppendLine("}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加最大长度项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendMinLengthText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.MinLength), true);
            if (objs.Length > 0)
            {
                Reflection.Model.Attribute.MinLength v = objs[0] as Reflection.Model.Attribute.MinLength;
                sb.AppendLine("if(" + p.Name + ".length<" + v.TheLength + "){");
                sb.AppendLine("jAlert(\"" + Entity.GetMinLengthMessage(p, obj, false) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("  return false;");
                sb.AppendLine("}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加浮点数项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendDecimalText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Decimal), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r = /^[\\-\\+]?(([0-9]+)([\\.,]([0-9]+))?|([\\.,]([0-9]+))?)$/");

                sb.AppendLine("if(" + p.Name + "!=\"\"){");

                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetDecimalMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");


                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加小数项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendDoubleText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Double), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r = /^[-\\+]?\\d\\d*\\.[0-9]\\d*$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetDoubleMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");

            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加小数项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendMobileText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Mobile), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r = /^(13[0-9]|15[0|1|2|3|5|6|7|8|9]|18[0|5|6|7|8|9])\\d{8}$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetMobileMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加小数项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendIDCardText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.IDCard), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r = /^(\\d{18,18}|\\d{15,15}|\\d{17,17}x)$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetIDCardMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加邮箱项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendEmailText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Email), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r =/^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetEmailMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加邮箱项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendMoneyText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Money), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r =/^(\\d{1,3},){0,}\\d{1,3}\\.\\d{2}$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetMoneyMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加时间项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendTimeText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Time), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r =/^(([01]\\d)|(2[0-3])):[0-5]\\d(:[0-5]\\d)?$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetTimeMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加日期项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendDateText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Date), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r =/((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(10|12|0?[13578])([-\\/\\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(11|0?[469])([-\\/\\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(0?2)([-\\/\\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([3579][26]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$))/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetDateMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");
            }
            return sb;
        }


        /// <summary>
        /// 为JS文件添加邮箱项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendZipCodeText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.ZipCode), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r =/^[1-9]\\d{5}$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetZipCodeMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加URL项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendURLText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Url), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r =/^[a-zA-Z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\s*)?$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetUrlMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加整数项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendNumberText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.Number), true);
            if (objs.Length > 0)
            {
                sb.AppendLine("var r =/^-?\\d+$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")!=true){");
                sb.AppendLine("jAlert(\"" + Entity.GetNumberMessage(p, obj) + "\",\"提示\");");
                sb.AppendLine("  $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("   return false;");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");
            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加最大值项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendMaxValueText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.MaxValue), true);
            if (objs.Length > 0)
            {

                Reflection.Model.Attribute.MaxValue v = objs[0] as Reflection.Model.Attribute.MaxValue;
                sb.AppendLine("var r =/^-?\\d+$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")==true){");
                sb.AppendLine("     if(" + p.Name + ">" + v.TheValue + "){");
                sb.AppendLine("jAlert(\"" + Entity.GetMaxValueMessage(p, obj, false) + "\",\"提示\");");
                sb.AppendLine("       $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("        return false;");
                sb.AppendLine("     }");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");

            }
            return sb;
        }

        /// <summary>
        /// 为JS文件添加最小值项判断
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private StringBuilder AppendMinValueText(StringBuilder sb, PropertyInfo p, object obj)
        {
            Object[] objs = p.GetCustomAttributes(typeof(Reflection.Model.Attribute.MinValue), true);
            if (objs.Length > 0)
            {
                Reflection.Model.Attribute.MinValue v = objs[0] as Reflection.Model.Attribute.MinValue;
                sb.AppendLine("var r =/^-?\\d+$/");
                sb.AppendLine("if(" + p.Name + "!=\"\"){");
                sb.AppendLine("if(r.test(" + p.Name + ")==true){");
                sb.AppendLine("     if(" + p.Name + "<" + v.TheValue + "){");
                sb.AppendLine("jAlert(\"" + Entity.GetMinValueMessage(p, obj, false) + "\",\"提示\");");
                sb.AppendLine("       $(\"#" + p.Name + "\").focus();");
                sb.AppendLine("        return false;");
                sb.AppendLine("     }");
                sb.AppendLine("}");
                sb.AppendLine("}else{");
                sb.AppendLine("return true;}");

            }
            return sb;
        }
    }
}
